home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT61.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  6.6 KB  |  252 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 61                         
  5.                                            
  6.  This program demonstrates use of the sprite library to create a simple       
  7.  SPACE INVADERS game. Mouse movement is used to control the user's ship,  
  8.  and the aliens will follow a predetermined pattern. Alien fire will not     
  9.  damage your ship. This game is incomplete.                                  
  10.                                           
  11.  *** PROJECT ***                                                             
  12.  This program requires the WGT5_WC.LIB and WSPR_WC.LIB files to be linked.   
  13.                                           
  14.  *** DATA FILES ***                                                          
  15.  INVADE.PAK, INVADER.SPR                                                     
  16.                                WATCOM C++ VERSION 
  17. ==============================================================================
  18. */
  19.  
  20. #include <stdlib.h>
  21. #include <conio.h>
  22. #include <dos.h>
  23. #include <ctype.h>
  24. #include <wgt5.h>
  25. #include <wgtspr.h>
  26.  
  27. block title;
  28.  
  29. color palette[256];
  30. block sprites[1001];
  31. short x,y,i;
  32. char k;
  33. char ch;
  34. short killed;
  35. short totshoot;
  36. short temp;
  37.  
  38. // sprites numbers (So you don't get confused)
  39. // 1    = spaceship
  40. // 2    = missile fired
  41. // 3-24 = aliens!
  42. // 25-39= aliens missiles
  43.  
  44. void gameloop (void);
  45.  
  46.  
  47. int timer;
  48.  
  49. void timer_routine (void)
  50. {
  51.  timer++;
  52. }
  53.  
  54.  
  55. void main(void)
  56. {
  57.   if (!vgadetected ())
  58.   {
  59.     printf ("VGA is required to run this program...");
  60.     exit (1);
  61.   }
  62.  
  63.  
  64.   printf ("WGT Example #61\n\n");
  65.   printf ("This program demonstrates use of the sprite library to create a simple\n");
  66.   printf ("SPACE INVADERS game. Mouse movement is used to control the user's ship,\n");
  67.   printf ("and the aliens will follow a predetermined pattern. Alien fire will not\n");
  68.   printf ("damage your ship. This game is incomplete.\n");
  69.   printf ("Press q during the game to quit.\n");
  70.  
  71.   i = minit ();
  72.   if (i == 0)
  73.   { 
  74.     printf ("Mouse not detected.  You need a mouse for this example program\n");
  75.     exit (1);
  76.   }
  77.   else printf ("Mouse with %i buttons detected.\n", i);
  78.  
  79.   printf ("\nPress any key to begin.\n");
  80.   getch ();
  81.  
  82.   vga256 ();
  83.   title = wloadpak ("invade.pak");
  84.   wloadsprites (palette, "invader.spr", sprites, 0, 100);
  85.   wsetpalette (0, 255, palette);
  86.  
  87.   winittimer ();
  88.   wstarttimer (timer_routine, TICKS(60));
  89.  
  90.   do {
  91.   killed = 0;
  92.   wclip (0, 0, 319, 199);
  93.   maxsprite = 39;                            /* thirty nine sprites on now */
  94.   initialize_sprites (sprites);
  95.  
  96.   wsetscreen (spritescreen);
  97.  
  98.   wputblock (0, 0, title, 0);
  99.   wsetcolor (0);
  100.   wbar(0, 0, 319, 140);
  101.   wnormscreen ();
  102.   wcopyscreen(0, 0, 319, 199, spritescreen, 0, 0, NULL);
  103.   wcopyscreen(0, 0, 319, 199, spritescreen, 0, 0, backgroundscreen);
  104.  
  105.   spriteon(0, 160, 148, 1);
  106.  
  107.   for (y = 0; y < 3; y++)
  108.     for (x = 1; x < 8; x++)
  109.     {
  110.       temp = (y * 7) + x + 1;
  111.       spriteon(temp, x * 20 + 10, 20 + y * 30, 3);   /* turn alien on */
  112.       animate(temp, "(3,3)(4,3)(5,3)(4,3)R");        /* animate it */
  113.       animon (temp);                                        
  114.       movex (temp, "(1,150,0)(0,30,0)(-1,150,0)(0,30,0)R");   
  115.     /* set up x movement */
  116.       
  117.       movey (temp, "(0,150,0)(1,30,0)(0,150,0)(-1,30,0)R");   
  118.     /* set up y movement */
  119.       movexon (temp);
  120.       moveyon (temp);
  121.     }
  122.     
  123.   msetbounds(0, 148, 319, 148);
  124.   msetxy (160, 148);
  125.  
  126.   timer = 0;
  127.  
  128.   for (x = 1; x < 15; x++)
  129.     spriteoff (x + 22);
  130.  
  131.   totshoot = 0;
  132.  
  133.  
  134.     do {                                /* Play game until keypress */
  135.       gameloop ();
  136.     } while ((!kbhit ()) && (killed < 21));
  137.     
  138.     if (killed != 21)
  139.       ch = toupper (getch ());
  140.     killed = 0;
  141.     msetbounds (0, 0, 319, 199);
  142.   
  143.     deinitialize_sprites ();            /* Deinit the sprite system */
  144.   } while (ch != 'Q');                  /* Restart game if not Q */
  145.  
  146.   wstoptimer ();
  147.   wdonetimer ();
  148.  
  149.   mdeinit ();
  150.   wsetmode (3);
  151. }
  152.  
  153.  
  154.  
  155. void gameloop (void)
  156. {
  157. short checkspr;
  158. int movement_multiplier;
  159. int count;
  160. int openslot;
  161.  
  162.   //wretrace ();                        /* Time updates to vertical retrace */
  163.   erase_sprites ();
  164.  
  165.   count = movement_multiplier = timer;
  166.   timer = 0;
  167.  
  168.  
  169.   while (count > 0)
  170.     {
  171.      s[0].x = mouse.mx;                /* Set player position */
  172.      s[0].y = mouse.my;
  173.  
  174.      if ((mouse.but == 1) && (s[1].on == 0))   /* not shooting then */
  175.        {
  176.     sound (600);
  177.     spriteon (1, s[0].x + 3, s[0].y, 2);/* turn on the missile sprite */
  178.     movey (1, "(-2,200,0)");       /* make it move up */
  179.     moveyon (1);                   /* turn the movement on */
  180.        }
  181.     
  182.      for (y = 0; y < 3; y++)           /* loop through all aliens */
  183.        for (x = 1; x < 8; x++)
  184.      {
  185.       checkspr = (y * 7) + x + 1;
  186.       if (s[checkspr].on == 1)     /* if the sprite is still onscreen */
  187.         {
  188.          /* Do not shoot more than 14 at once since we can only have 
  189.         sprites */
  190.          if ((rand () % 50 == 5) && (totshoot < 13))
  191.            /* 1 in 50 chance of shooting */
  192.            {       
  193.         openslot = -100;
  194.         i = 1;
  195.         do 
  196.           {
  197.            if (s[i + 22].on == 0)  /* find an open slot for missile */
  198.              { 
  199.               openslot = i;
  200.               i = 16;
  201.              }
  202.            i++;
  203.           } while (i < 15);
  204.       
  205.         if (openslot != -100)
  206.           {
  207.            totshoot++;
  208.            spriteon (openslot + 22, s[checkspr].x + 4, 
  209.                  s[checkspr].y, 2);  /* turn shot on */
  210.            movey (openslot + 22, "(1,160,0)"); /* move it down */
  211.            moveyon (openslot + 22);
  212.           }
  213.            } 
  214.     
  215.       if ((overlap (1, checkspr) == 1) && (s[checkspr].num <6))        
  216.       /* if your missile is hitting it and it isn't already exploding */
  217.         {
  218.          sound (200);
  219.          spriteoff (1);                     /* turn off the missile */
  220.          animate (checkspr, "(6,5)(7,5)(8,5)(9,5)(10,5)");  
  221.               /* show explosion */
  222.          animon (checkspr);                 /* reset animation */
  223.         }
  224.       if (s[checkspr].num == 10)            /* if finished animating, */
  225.         {
  226.          spriteoff (checkspr);              /* turn off the sprite */
  227.          killed++;
  228.         }
  229.      }
  230.     }
  231.   
  232.   for (y = 1; y < 15; y++)              /* Disable missles when they hit ground */
  233.   {
  234.     if ((s[y + 22].on == 1) && (s[y + 22].y > 154))
  235.       {
  236.        spriteoff (y + 22);
  237.        totshoot--;
  238.       }
  239.   }
  240.   
  241.   if (s[1].y < -10)                     /* User missle off screen? */
  242.     spriteoff (1);
  243.   count--;
  244.  } 
  245.  
  246.   draw_sprites (movement_multiplier);                       
  247.   /* Draw the sprites again */
  248.    
  249.   nosound ();                            /* Stop all sounds */
  250. }
  251.  
  252.